package com.lastcrdg.textcustomview.CustomView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * author：liluo
 * e-mail：lastcrazysun@163.com
 * company：cusc
 * date：2017/10/9 15:12
 * description：
 */

public class TextBezier extends View {
    private int width;
    private int height;
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private PointF mStart = new PointF(0, 0);
    private PointF mEnd = new PointF(0, 0);
    private PointF mMove1 = new PointF(0, 0);
    private PointF mMove2 = new PointF(0, 0);
    private boolean isMove2Control;

    public boolean isMove2Control() {
        return isMove2Control;
    }

    public void setMove2Control(boolean move2Control) {
        isMove2Control = move2Control;
    }

    public TextBezier(Context context) {
        super(context);
    }

    public TextBezier(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public TextBezier(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initPaint() {
        mPaint.setColor(Color.GRAY);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(6);
    }

    private void changePaint() {
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        mStart.set(width / 2 - 200, height / 2);
        mEnd.set(width / 2 + 200, height / 2);
        mMove1.set(width / 2 - 200, height / 2 + 200);
        mMove2.set(width / 2 + 200, height / 2 + 200);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isMove2Control) {
            mMove1.set(event.getX(), event.getY());
        }else {
            mMove2.set(event.getX(), event.getY());
        }
        invalidate();
        initPaint();
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画线
        canvas.drawLine(mStart.x, mStart.y, mMove1.x, mMove1.y, mPaint);
        canvas.drawLine(mMove2.x, mMove2.y, mMove1.x, mMove1.y, mPaint);
        canvas.drawLine(mEnd.x, mEnd.y, mMove2.x, mMove2.y, mPaint);
        changePaint();
        //画点
        canvas.drawCircle(mStart.x, mStart.y, 10, mPaint);
        canvas.drawCircle(mEnd.x, mEnd.y, 10, mPaint);
        canvas.drawCircle(mMove1.x, mMove1.y, 10, mPaint);
        canvas.drawCircle(mMove2.x, mMove2.y, 10, mPaint);
        //画贝塞尔
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(4);
        Path path = new Path();
        path.moveTo(mStart.x, mStart.y);
        path.cubicTo(mMove1.x, mMove1.y, mMove2.x, mMove2.y, mEnd.x, mEnd.y);
        canvas.drawPath(path, mPaint);
    }
}
